package com.efast.dmom.mes.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.SupplierPart;
import com.efast.dmom.mes.entity.SupplierPartEntity;
import com.efast.dmom.mes.entity.base.IModificationFlags;
import com.efast.dmom.mes.service.SupplierPartService;

/**
 * 
 * @author WJ
 * @date modify 2020/2/21
 *
 */
@Controller
@RequestMapping(value = "/supplierPart")
public class SupplierPartController {
	@Autowired
	private SupplierPartService supplierPartService;

	@RequestMapping(value = "/operateBySupplierPart")
	public @ResponseBody ResultBean operateBySupplierPart(@RequestBody SupplierPart supplierPart) {
		
		SupplierPartEntity entity = supplierPart.getInnerData();
		// Determine the new
		if (supplierPart.getModificationFlag() == IModificationFlags.NEW) {
			return supplierPartService.save(supplierPart);
			// Judge update
		} else if (supplierPart.getModificationFlag() == IModificationFlags.MODIFIED) {
			return supplierPartService.update(supplierPart);
			// Judge to delete
		} else if (supplierPart.getModificationFlag() == IModificationFlags.DELETED) {
			return supplierPartService.delete(supplierPart);
		}
		return null;
	}

	// get supplier by id
	@RequestMapping(value = "/getSupplierPartById/{id}")
	public @ResponseBody SupplierPart getSupplierPartById(@PathVariable long id) {
		return supplierPartService.getById(id);
	}

	// Get the relationship between supplier and part through name
	@RequestMapping(value = "/getSupplierPartByName/{name}")
	public @ResponseBody SupplierPart getSupplierPartByName(@PathVariable String name) {
		return supplierPartService.getByName(name);
	}
	
	// Get the relationship between supplier and part through supplierId
	@RequestMapping(value = "/getSupplierPartBySupplierId/{supplierId}")
	public @ResponseBody List<SupplierPart> getSupplierPartBySupplierId(@PathVariable long supplierId) {
		return supplierPartService.getSupplierPartBySupplierId(supplierId);
	}

	// Get the relationship between supplier and part through partId
	@RequestMapping(value = "/getSupplierPartByPartId/{partId}")
	public @ResponseBody List<SupplierPart> getSupplierPartByPartId(@PathVariable long partId) {
		return supplierPartService.getSupplierPartByPartId(partId);
	}

}
